﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp.Service;
using AppCrawler;
using AppService.Comparer;
using AppService.Context;
using AppService.Dao;
using AppService.Extension;
using AppService.Model;

namespace WindowsFormsApp
{
    public partial class Form1 : Form
    {

        private  static readonly LuckyCrawler crawler =new LuckyCrawler();
        private static readonly DoubleColorBallDao dao=new DoubleColorBallDao();
        private static List<DoubleColorBall> listCache = new List<DoubleColorBall>();
        private DoubleColorBallService service;
        private ProgressBar progressBar = new ProgressBar();
        public Form1()
        {
            InitializeComponent();

            cbWeek.Items.Add("全部");
            cbWeek.Items.Add("星期一");
            cbWeek.Items.Add("星期二");
            cbWeek.Items.Add("星期三");
            cbWeek.Items.Add("星期四");
            cbWeek.Items.Add("星期五");
            cbWeek.Items.Add("星期六");
            cbWeek.Items.Add("星期日");
            cbWeek.SelectedIndex = 0;

            cbMonth.Items.Add("全部");
            cbMonth.Items.Add("01");
            cbMonth.Items.Add("02");
            cbMonth.Items.Add("03");
            cbMonth.Items.Add("04");
            cbMonth.Items.Add("05");
            cbMonth.Items.Add("06");
            cbMonth.Items.Add("07");
            cbMonth.Items.Add("08");
            cbMonth.Items.Add("09");
            cbMonth.Items.Add("10");
            cbMonth.Items.Add("11");
            cbMonth.Items.Add("12");
            cbMonth.SelectedIndex = 0;

            cbDay.Items.Add("全部");
            cbDay.Items.Add("01");
            cbDay.Items.Add("02");
            cbDay.Items.Add("03");
            cbDay.Items.Add("04");
            cbDay.Items.Add("05");
            cbDay.Items.Add("06");
            cbDay.Items.Add("07");
            cbDay.Items.Add("08");
            cbDay.Items.Add("09");
            cbDay.Items.Add("10");
            cbDay.Items.Add("11");
            cbDay.Items.Add("12");
            cbDay.Items.Add("13");
            cbDay.Items.Add("14");
            cbDay.Items.Add("15");
            cbDay.Items.Add("16");
            cbDay.Items.Add("17");
            cbDay.Items.Add("18");
            cbDay.Items.Add("19");
            cbDay.Items.Add("20");
            cbDay.Items.Add("21");
            cbDay.Items.Add("22");
            cbDay.Items.Add("23");
            cbDay.Items.Add("24");
            cbDay.Items.Add("25");
            cbDay.Items.Add("26");
            cbDay.Items.Add("27");
            cbDay.Items.Add("28");
            cbDay.Items.Add("29");
            cbDay.Items.Add("30");
            cbDay.Items.Add("31");
            cbDay.SelectedIndex = 0;

            progressBar.Width = 100;
            progressBar.Height = 10;
            progressBar.Minimum = 0;
            progressBar.Maximum = 100;
            progressBar.Value = 0;
            progressBar.Step = 1;

            progressBar.Visible = true;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listCache = dao.GetList().OrderByDescending(x => x.Issue).ToList();

            dataGridView1.DataSource = listCache;
        }
        private void UpdateDate()
        {
           
            progressBar.Show();
            var list = new List<DoubleColorBall>();
            var startYear = Convert.ToDateTime("2002-01-01");
            var endYear = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            while (startYear.Year != endYear.Year)
            {
                progressBar.PerformStep();
                startYear = startYear.AddYears(1);
                list.AddRange(crawler.GetDoubleColorBallAtWebForYear(startYear.ToString("yyyy")));
            }
            var differenceList = list.Except(listCache, new DoubleColorBallComparer()).ToList();
            if (differenceList.Count > 0)
            {
                dao.Save(differenceList);
            }
            listCache = dao.GetList().OrderByDescending(x => x.Issue).ToList();
            dataGridView1.BeginInvoke(new MethodInvoker(() => dataGridView1.DataSource=listCache));
          

            progressBar.Hide();
        }

        /// <summary>
        /// 每年同期号的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnIssue_Click(object sender, EventArgs e)
        {
            var list = listCache;
            var issue = txtIssue.Text;

            if (!string.IsNullOrWhiteSpace(issue))
            {
                list = listCache.Where(x => x.Issue.Substring(4) == issue).ToList();
            }
            var res = Probability(list);
        }

        private void btnMonth_Click(object sender, EventArgs e)
        {
            var list = listCache;
            var month = cbMonth.SelectedItem.ToString();
            var day = cbDay.SelectedItem.ToString();
            if (month != "全部")
            {
                list = listCache.Where(x => x.OpenDate.ToString("MM") == month).ToList();
            }
            if (day != "全部")
            {
                list = listCache.Where(x => x.OpenDate.ToString("dd") == day).ToList();
            }
            var res = Probability(list);
        }
        private void btnWeek_Click(object sender, EventArgs e)
        {
            var list = listCache;
            var week = cbWeek.SelectedItem.ToString();
            if (week != "全部")
            {
                list = listCache.Where(x => x.Week == week).ToList();
            }
            var res= Probability(list);

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Thread thread=new Thread(UpdateDate);
            thread.Start();
        }

        /// <summary>
        /// 出现的几率
        /// </summary>
        /// <param name="list"></param>
        /// <param name="week"></param>
        /// <param name="month"></param>
        /// <param name="issue"></param>
        private void Probability(List<DoubleColorBall> list,string week="星期一",string month="",string issue="081")
        {
            Dictionary<string, int> dic = new Dictionary<string, int>
            {
                {"01", 0}, {"02", 0}, {"03", 0}, {"04", 0}, {"05", 0}, {"06", 0}, {"07", 0}, {"08", 0}, {"09", 0},
                {"10", 0}, {"11", 0}, {"12", 0}, {"13", 0}, {"14", 0}, {"15", 0}, {"16", 0}, {"17", 0}, {"18", 0}, {"19", 0},
                {"20", 0}, {"21", 0}, {"22", 0}, {"23", 0}, {"24", 0}, {"25", 0}, {"26", 0}, {"27", 0}, {"28", 0}, {"29", 0},
                {"30", 0}, {"31", 0}, {"32", 0}, {"33", 0}
            };
            Dictionary<string, int> dicBlue = new Dictionary<string, int>
            {
                {"01", 0}, {"02", 0}, {"03", 0}, {"04", 0}, {"05", 0}, {"06", 0}, {"07", 0}, {"08", 0}, {"09", 0},
                {"10", 0}, {"11", 0}, {"12", 0}, {"13", 0}, {"14", 0}, {"15", 0}, {"16", 0}
            };
            Dictionary<string, int> dicWeek=new Dictionary<string, int>(dic);
            Dictionary<string, int> dicWeekBlue=new Dictionary<string, int>(dicBlue);
            Dictionary<string, int> dicMonth=new Dictionary<string, int>(dic);
            Dictionary<string, int> dicMonthBlue = new Dictionary<string, int>(dicBlue);
            Dictionary<string, int> dicIssue=new Dictionary<string, int>(dic);
            Dictionary<string, int> dicIssueBlue = new Dictionary<string, int>(dicBlue);



            #region 所有的数字出现的概率
            for (int i = 0; i < list.Count; i++)
            {
                var tempRedArray = list[i].RedBall.Split(',');
                for (int j = 0; j < tempRedArray.Length; j++)
                {
                    dic[tempRedArray[j]] += 1;
                }

                dicBlue[list[i].BlueBall] += 1;
            }
            Dictionary<string, float> resList = new Dictionary<string, float>();
            Dictionary<string, float> resBlueList = new Dictionary<string, float>();
            foreach (var d in dic)
            {
                resList.Add(d.Key, (d.Value / (list.Count * 1.00f)));
            }

            foreach (var b in dicBlue)
            {
                resBlueList.Add(b.Key, (b.Value / (list.Count * 1.00f)));
            }
            var nn = resList.OrderByDescending(x => x.Value);
            var nnBlue= resBlueList.OrderByDescending(x => x.Value);
            #endregion

            #region 某个星期的数字出现的概率
            var weekList = list.Where(x => x.Week == week).ToList();
            for (int i = 0; i < weekList.Count; i++)
            {
                var tempRedArray = weekList[i].RedBall.Split(',');
                for (int j = 0; j < tempRedArray.Length; j++)
                {
                    dicWeek[tempRedArray[j]] += 1;
                }

                dicWeekBlue[weekList[i].BlueBall] += 1;
            }
            Dictionary<string, float> resWeekList = new Dictionary<string, float>();
            Dictionary<string, float> resWeekBlueList = new Dictionary<string, float>();
            foreach (var d in dic)
            {
                resWeekList.Add(d.Key, (d.Value / (weekList.Count * 1.00f)));
            }
            foreach (var b in dicWeekBlue)
            {
                resWeekBlueList.Add(b.Key, (b.Value / (weekList.Count * 1.00f)));
            }

            var nnWeek = resWeekList.OrderByDescending(x => x.Value);
            var nnWeekBlue=resWeekBlueList.OrderByDescending(x => x.Value);
            #endregion

            #region 某个日期的数字出现的概率
            if (!string.IsNullOrEmpty(month))
            {
                var monthList = list.Where(x => x.OpenDate.ToString("MM-dd") == month).ToList();
                for (int i = 0; i < monthList.Count; i++)
                {
                    var tempRedArray = monthList[i].RedBall.Split(',');
                    for (int j = 0; j < tempRedArray.Length; j++)
                    {
                        dicMonth[tempRedArray[j]] += 1;
                    }

                    dicMonthBlue[monthList[i].BlueBall] += 1;
                }
                Dictionary<string, float> resMonthList = new Dictionary<string, float>();
                Dictionary<string, float> resMonthBlueList = new Dictionary<string, float>();
                foreach (var d in dic)
                {
                    resMonthList.Add(d.Key, (d.Value / (monthList.Count * 1.00f)));
                }

                foreach (var b in dicMonthBlue)
                {
                    resWeekBlueList.Add(b.Key, (b.Value / (monthList.Count * 1.00f)));
                }
                var nnMonth = resMonthList.OrderByDescending(x => x.Value);
                var nnMonthBlue = resWeekBlueList.OrderByDescending(x => x.Value);
            }


            #endregion

            #region 某个期号的数字出现的概率
            if (!string.IsNullOrEmpty(issue))
            {
                var issueList = list.Where(x => x.Issue.Substring(4) == issue).ToList();
                for (int i = 0; i < issueList.Count; i++)
                {
                    var tempRedArray = issueList[i].RedBall.Split(',');
                    for (int j = 0; j < tempRedArray.Length; j++)
                    {
                        dicIssue[tempRedArray[j]] += 1;
                    }

                    dicIssueBlue[issueList[i].BlueBall] += 1;
                }
                Dictionary<string, float> resIssueList = new Dictionary<string, float>();
                Dictionary<string, float> resIssueBlueList = new Dictionary<string, float>();
                foreach (var d in dic)
                {
                    resIssueList.Add(d.Key, (d.Value / (issueList.Count * 1.00f)));
                }

                foreach (var b in dicIssueBlue)
                {
                    resIssueBlueList.Add(b.Key, (b.Value / (issueList.Count * 1.00f)));
                }
                var nnIssue = resIssueList.OrderByDescending(x => x.Value);
                var nnIssueBlue = resIssueBlueList.OrderByDescending(x => x.Value);
            }


            #endregion






            //var sb = new StringBuilder();
            //foreach (var d in nn)
            //{
            //    sb.AppendLine($"{d.Key}:{d.Value*100:F2}%");
            //}

            //lblLast.Text = sb.ToString();
        }

        /// <summary>
        /// 几率运算
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private Tuple<Dictionary<string, float>, Dictionary<string, float>> Probability(List<DoubleColorBall> list)
        {
            Dictionary<string, int> dicRed = new Dictionary<string, int>
            {
                {"01", 0}, {"02", 0}, {"03", 0}, {"04", 0}, {"05", 0}, {"06", 0}, {"07", 0}, {"08", 0}, {"09", 0},
                {"10", 0}, {"11", 0}, {"12", 0}, {"13", 0}, {"14", 0}, {"15", 0}, {"16", 0}, {"17", 0}, {"18", 0}, {"19", 0},
                {"20", 0}, {"21", 0}, {"22", 0}, {"23", 0}, {"24", 0}, {"25", 0}, {"26", 0}, {"27", 0}, {"28", 0}, {"29", 0},
                {"30", 0}, {"31", 0}, {"32", 0}, {"33", 0}
            };
            Dictionary<string, int> dicBlue = new Dictionary<string, int>
            {
                {"01", 0}, {"02", 0}, {"03", 0}, {"04", 0}, {"05", 0}, {"06", 0}, {"07", 0}, {"08", 0}, {"09", 0},
                {"10", 0}, {"11", 0}, {"12", 0}, {"13", 0}, {"14", 0}, {"15", 0}, {"16", 0}
            };
            for (int i = 0; i < list.Count; i++)
            {
                var tempRedArray = list[i].RedBall.Split(',');
                for (int j = 0; j < tempRedArray.Length; j++)
                {
                    dicRed[tempRedArray[j]] += 1;
                }

                dicBlue[list[i].BlueBall] += 1;
            }
            Dictionary<string, float> red = new Dictionary<string, float>();
            Dictionary<string, float> blue = new Dictionary<string, float>();
            foreach (var d in dicRed)
            {
                red.Add(d.Key, (d.Value / (list.Count * 1.00f)));
            }

            foreach (var b in dicBlue)
            {
                blue.Add(b.Key, (b.Value / (list.Count * 1.00f)));
            }
            return Tuple.Create(red, blue);
        }


    }
}